package com.zymail.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zymail.bean.ZyComment;
import com.zymail.bean.ZyCommentImg;
import com.zymail.service.ZyCommentImgService;
import com.zymail.service.ZyCommentService;
import com.zymail.service.ZyGoodImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/comment")
public class CommentController extends BaseController{
    @Autowired
    public ZyCommentService commentService;

    @Autowired
    public ZyCommentImgService imgService;

    /**
     * 得到一个商品的所有评论接口
     * @param id 商品id
     * @return  List<ZyComment></>
     */
    @RequestMapping("/getgoodallcomment/{id}")
    public List<ZyComment> getGoodAllComment(@PathVariable("id") Integer id){
        return commentService.getall(id);
    }

    /**
     * 得到一个商品的最新的两个评论
     * @param id 商品id
     * @return  List<ZyComment></>
     */
    @RequestMapping("/getgoodtwocomment/{id}")
    public List<ZyComment> getGoodTwoComment(@PathVariable("id") Integer id){
        return commentService.gettwo(id);
    }

    /**
     * 上传文件
     * @param file  需要上传的文件
     * @param model model
     * @param request   httprequest
     * @return  filepath
     */
    @PostMapping(value = "/fileUpload")
    public String fileUpload(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request) {
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = "C:\\Users\\12785\\Desktop\\web\\zhuiyuan-mall\\file\\"; // 上传后的路径
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = "/temp-rainy/" + fileName;
        model.addAttribute("filename", filename);
        return filename;
    }

    /**
     * 上传评论
     * @param session_key   session_key
     * @param comment   评论实体
     * @return  评论id
     */
    @PostMapping(value = "/create/{session_key}")
    public Integer create_comment(@PathVariable String session_key, @RequestBody ZyComment comment){
        Integer userId = get_userId(session_key);
        comment.setTime(LocalDateTime.now());
        comment.setUserId(userId);
        if(commentService.save(comment)){
            return comment.getId();
        }

        return -1;
    }

    /**
     * 绑定图片
     * @param imgList  img形成的list
     * @return  bool
     */
    @PostMapping(value = "/bindimg")
    public boolean bindImg(@RequestBody List<ZyCommentImg> imgList){
        return imgService.saveBatch(imgList);
    }

    /**
     * 通过id得到comment
     * @param commentId commentid
     * @return  comment实体
     */
    @GetMapping("/getComment/{commentId}")
    public ZyComment getComment(@PathVariable Integer commentId){
        return commentService.getById(commentId);
    }

    /**
     * 得到自己的评论
     * @param session_key   session_key
     * @return  list
     */
    @GetMapping("/getusercomment/{session_key}")
    public List<ZyComment> getComemntsByUserId(@PathVariable String session_key){
        Integer userId = get_userId(session_key);
        if(userId == 0) return null;

        QueryWrapper<ZyComment> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return commentService.list(wrapper);
    }

    /**
     * 删除自己的评论
     * @param commentId id
     * @return  bool
     */
    @DeleteMapping("/dele/{commentId}")
    public boolean dele(@PathVariable Integer commentId){
        return commentService.removeById(commentId);
    }

    /**
     * 得到和订单绑定的评论
     * @param orderId   orderid
     * @return  comment实体
     */
    @GetMapping("/getordercom/{orderId}")
    public ZyComment getordercomment(@PathVariable Integer orderId){
        QueryWrapper<ZyComment> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        return commentService.getOne(wrapper);
    }

    @RequestMapping("/all")
    public Map<String,Object> all(){
        Map<String,Object> map = new HashMap<>();
        List<ZyComment> zyComments = commentService.list();
        map.put("code",0);
        map.put("data",zyComments);
        return map;
    }
}
